package bawei.com.bilibili;


import android.content.res.AssetManager;


import android.graphics.Color;
import android.os.Bundle;


import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;


import android.support.v7.widget.Toolbar;



import com.flyco.tablayout.SlidingTabLayout;


import com.google.gson.Gson;


import java.io.BufferedReader;
import java.io.InputStreamReader;



import java.util.ArrayList;
import java.util.List;



import bawei.com.adapter.RegionTypeDetailsPageAdapter;
import bawei.com.model.bean.ReginBean;
import bawei.com.widget.NoScrollViewPager;

/**
 * 类描述:
 * 作者：cwm
 * 时间:2017/3/21 18:13
 * 邮箱:18310832074@163.com
 */

public class RegionTypeDetailsActivity extends FragmentActivity{

    Toolbar mToolbar;
    NoScrollViewPager mViewPager;
    SlidingTabLayout mSlidingTab;
    List<ReginBean.DataBean.ChildrenBean> childrenBeen=new ArrayList<>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_region_details);
        mToolbar= (Toolbar) findViewById(R.id.toolbar);
        mViewPager= (NoScrollViewPager) findViewById(R.id.view_pager);
        mSlidingTab= (SlidingTabLayout) findViewById(R.id.sliding_tabs);
        getdate();

    }

    public void getdate(){

        String s = initAssets();
        ReginBean reginBean = new Gson().fromJson(s, ReginBean.class);
        List<ReginBean.DataBean> data = reginBean.getData();
        String name = getIntent().getStringExtra("name");
        mToolbar.setTitle(name);
        mToolbar.setLogo(R.drawable.action_button_back_pressed_light);
        mToolbar.inflateMenu(R.menu.menu_rank);
        for (int i = 0; i < data.size(); i++) {
            if(name.equals(data.get(i).getName())){
                childrenBeen.addAll(data.get(i).getChildren());
            }
        }
        RegionTypeDetailsPageAdapter  adapter=new RegionTypeDetailsPageAdapter(getSupportFragmentManager(),childrenBeen);
        mViewPager.setAdapter(adapter);
        mViewPager.setOffscreenPageLimit(childrenBeen.size());
        mSlidingTab.setViewPager(mViewPager);
        mSlidingTab.setTextSelectColor(Color.WHITE);
        mSlidingTab.setTextUnselectColor(Color.GRAY);
        mSlidingTab.setUnderlineColor(Color.WHITE);
    }

    private String initAssets() {

        StringBuilder stringBuilder = new StringBuilder();

        AssetManager assets = getAssets();
        try {
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    assets.open("region.json")));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();

    }

}
